#! /bin/bash

containers=("zabbix-mysql" "zabbix-java-gateway" "zabbix-server-mysql" "zabbix-web-nginx-mysql")

Init(){
    basepath=$(cd `dirname $0`; pwd)
    echo "Create and Initializa all containers"
    ${basepath}/init.sh
}

Restart(){
    Control "stop"
    Control "start"
}

Control(){
    if [ "$1" == "stop" ]; then
        for ((i=3;i>=0;i--)); do
            echo "docker stop ${containers[i]}"
            docker stop ${containers[i]}
        done
    else
        for ((i=0;i<4;i++)); do
            echo "docker $1 ${containers[i]}"
            docker $1 ${containers[i]}
        done
    fi
}


help (){
    echo "This is controller script for Zabbix containers."
    echo
    echo "Note: must run --init after --stop and --rm"
    echo 
    echo "controller.sh --[init|stop|rm|start|restart]"
    echo "-i, --init            Create new 4 containers and initialize them."
    echo "-c, --create"
    echo "-s, --stop            Stop all containers."
    echo "-r, --remove          Remove all containers. Please stop them manual."
    echo "-l, --logs            Show all containers' log."
    echo "-h, --help            Show this help."
    
    echo 
    echo "--restart             Restart all containers."
    
}

if [ -n "$1" ]; then
    case "$1" in
        -i|-c|--init|--create|init|create) Init;;
        -s|--stop|stop) Control "stop";;
        -r|--remove|remove) Control "rm";;
        -l|--logs|logs) Control "logs";;
        --restart|restart) Restart;;
        -h|--help) help;;
        *) echo "Error parameters." && exit 1;;
    esac
fi